<?php
declare (strict_types = 1);

namespace app\week2\controller;

use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\View;
use think\Image;
use think\Request;

class Article
{
   /**
    * 展示添加的表单
    */
   public function form(){

       // 分类动态展示

       $category = Db::table('category')->select();

       View::assign('category',$category);

        return View::fetch('/add');
   }

    /**
     * 执行添加
     */
   public function add(){

       try {
           $param = \request()->all();
           $param['title'] = trim($param['title']);


           validate(\app\week2\validate\Article::class)->check($param);

           $path = Filesystem::disk('public')->putFile('image',$param['logo']);

           $image = Image::open('./'.$path);
           $image->thumb(150,105)->text('第二周周考',getcwd().'/static/font.ttf',20,'#FF0000')->save('./'.$path);
            return redirect();
       }catch (ValidateException $validateException) {
           dd($validateException->getError());
       }
        dd(\request()->all());
   }

    /**
     * 列表展示
     */
   public function list(){
       $title = \request()->get('title');
       $data = Article::join('category','category.id=article.category_id')->paginate([
           'list_rows' => 4,
           'query' => [
               'title' => $title
           ],
       ]);
   }

    /**
     * 软删除
     */
   public function del(){
       $id =\request()->get('id');
       if (!is_numeric($id)) {
           dd("参数错误");
       }
       Article::destory($id);
   }
   /**
    *
    * 回收站
    */

   public function web(){
       # 查询已经删除的数据
        Article::onlyTrashed()->select();
   }

   public function restore(){
       $id = \request()->get('id');
       $article = Article::onlyTrashed()->find($id);
       $article->restore();
   }
}
